package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    //处理查询所有部门的请求,响应HTML
    //employee/*.do
    @RequiredPermission(name = "员工页面", expression = "employee:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);

        PageInfo<Employee> query = employeeService.query(qo);
        model.addAttribute("pageInfo", query);
        return "employee/list";// 找视图 /WEB-INF/views/employee/list.jsp
    }

    //验证用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String username) {
        HashMap<String, Boolean> map = new HashMap<>();
        Employee e = employeeService.selectByUername(username);
        //因为插件需要的值是key为valid的键值对,所以我们给他键值对
        map.put("valid", e == null);//判断数据库找的值是否为空,为空就为true
        return map;
    }

    //处理删除请求
    @RequiredPermission(name = "员工删除", expression = "employee:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            employeeService.delete(id);
        }
//让浏览器重新发送一次请求
        return "redirect:/employee/list";

    } //去新增或者去修改页面

    @RequiredPermission(name = "员工新增/修改", expression = "employee:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Model model, Long id) {
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments", departments);
        if (id != null) {//表示去修改
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);
        }
        List<Role> roles = roleService.listAll();
        model.addAttribute("roles", roles);

        List<Role> rolesByEid = roleService.selectByRid(id);
        model.addAttribute("rolesByEid", rolesByEid);

        return "employee/input";//让浏览器重新发一次请求
    }

    //新增或者修改
    @RequiredPermission(name = "员工新增/修改", expression = "employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() == null) {
            //新增
            employeeService.save(employee, ids);
        } else {
            //修改
            employeeService.update(employee, ids);
        }

        return new JsonResult();//让浏览器重新发送一次请求
    }


    @RequiredPermission(name = "员工导入", expression = "employee:exportXls")
    @RequestMapping("/exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置响应头
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        Workbook workbook = employeeService.exportXls();
        workbook.write(response.getOutputStream());

    }


    @RequiredPermission(name = "员工导入", expression = "employee:importXls")
    @RequestMapping("/importXls")
    public String importXls(MultipartFile file) throws IOException {
        System.out.println("file.getName() = " + file.getName());
        employeeService.importXls(file);
        return "redirect:/employee/list";
    }

    @RequiredPermission(name = "员工重置密码", expression = "employee:updatePassword")
    @RequestMapping("/updatePassword")
    public String updatePassword(Long id) throws IOException {
        employeeService.updatePassword(id);
        return "redirect:/employee/list";
    }

    //查所有员工
    @RequestMapping("/selectedAll")
    @ResponseBody
    public List<Employee> selectedAll(Model model) throws IOException {
        return  employeeService.selectedAll();
    }



}


